package mo.leetcode_top;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class L797 {

    private List<List<Integer>> res = new ArrayList<>();
    private Stack<Integer> path = new Stack<>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {

        path.push(0);
        dfs(graph, 0, graph.length - 1);
        return res;

    }


    private void dfs(int[][] graph, int x, int n) {
        if (x == n) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int e : graph[x]) {
            path.push(e);
            dfs(graph, e, n);
            path.pop();
        }
    }
}
